package com.ukao.steward.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseDialogFragment;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.listener.DialogSubmitListener;
import com.ukao.steward.listener.MyTextWatcher;
import com.ukao.steward.util.ActivityUtils;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.widget.StateButton;
import com.ukao.steward.widget.StateImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.steward.R.id.hite;
import static com.ukao.steward.R.id.sure_btn;
import static com.ukao.steward.R.id.view;
import static com.ukao.steward.consts.Constant.ARG_PARAM3;
import static com.ukao.steward.consts.Constant.REQUEST_CODE;


/**
 * 取件对话框
 */
public class UnTakeDialogFragment extends BaseDialogFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.close_btn)
    ImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.dialog_msg)
    TextView dialogMsg;
    @BindView(R.id.sweep_btn)
    StateImageView sweepBtn;
    @BindView(R.id.edit_text)
    EditText editText;
    @BindView(sure_btn)
    StateButton sureBtn;


    private String mParam1;
    private String mParam2;
    private int type;
    private int viewId;
    private Unbinder unbinder;
    private boolean islogistics;

    private  String  mTitle;
    private String mCancelButtonTitle="取消";
    private String mSubmitButtonTitle="确定";
    private  String  mdialogMsg="订单金额";
    private  boolean  isVisibilityDialogMsg=false;
    private  boolean isInputInt=true;  /*是否整数类型*/
    private String mEditHint;


    public static UnTakeDialogFragment newInstance(String mTitle,String  mSubmitButtonTitle,String  mdialogMsg,boolean
            isVisibilityDialogMsg,boolean  isInputInt,int viewId,String  mEditHint) {
        UnTakeDialogFragment fragment = new UnTakeDialogFragment();
        Bundle args = new Bundle();
        args.putString("mTitle", mTitle);
        args.putString("mEditHint", mEditHint);
        args.putString("mSubmitButtonTitle", mSubmitButtonTitle);
        args.putString("mdialogMsg", mdialogMsg);
        args.putBoolean("isVisibilityDialogMsg", isVisibilityDialogMsg);
        args.putBoolean("isInputInt", isInputInt);
        args.putInt("viewId", viewId);
        fragment.setArguments(args);
        return fragment;
    }







    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            Bundle args = getArguments();
            mTitle=args.getString("mTitle", mTitle);
            mSubmitButtonTitle=args.getString("mSubmitButtonTitle" );
            mdialogMsg=args.getString("mdialogMsg");
            isVisibilityDialogMsg=args.getBoolean("isVisibilityDialogMsg");
            isInputInt=args.getBoolean("isInputInt");
            viewId = getArguments().getInt("viewId");
            mEditHint=args.getString("mEditHint");

        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_untake_layout, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        setShowBottom(true);
        setIsWithWrap(false);
        if(isInputInt){
            editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
        }else{
            editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        }
        editText.setHint(mEditHint);
        title.setText(mTitle);
        dialogMsg.setVisibility(isVisibilityDialogMsg?View.VISIBLE:View.GONE);
        sureBtn.setText(mSubmitButtonTitle);
        dialogMsg.setText(mdialogMsg);



    }

    private TextWatcher mTextWatcher = new MyTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            Constant.calculate(s.toString(), editText);
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }

    @OnClick({R.id.close_btn, R.id.sweep_btn, sure_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.sweep_btn:
                Intent mIntent = ActivityUtils.JumpToBundleActivity(activity);
                startActivityForResult(mIntent ,Constant.REQUEST_CODE);

                break;
            case sure_btn:
                String streditText = getText(editText);
                if (CheckUtils.isEmpty(streditText)) return;

                if (mDialogSubmitListener != null) {
                    mDialogSubmitListener.onSubmitClick(viewId, streditText);
                }
                dismiss();

                break;
        }
    }





    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == activity.RESULT_OK) {

            switch (requestCode) {
                case Constant.REQUEST_CODE:
                    String  code=data.getStringExtra(Constant.ARG_PARAM1);
                    editText.setText(code);
                    break;
            }
        }

    }

    public static Builder createBuilder(
            FragmentManager fragmentManager) {
        return new Builder(fragmentManager);
    }
    public  static   class   Builder{
        private final FragmentManager mFragmentManager;
        private  String  mTitle;

        private String mSubmitButtonTitle="确定";
        private  String  mdialogMsg="订单金额";
        private  boolean  isVisibilityDialogMsg=false;
        private  boolean isInputInt=true;  /*是否整数类型*/
        public DialogSubmitListener mListener;
        private   int  viewId;
        private String mTag = "UnTakeDialogFragment";
       private  String   mEditHint="";

        public   Builder(FragmentManager fragmentManager){
            mFragmentManager=fragmentManager;
        }

        public Builder setmTitle(String mTitle) {
            this.mTitle = mTitle;

            return  this;
        }


        public Builder setmSubmitButtonTitle(String mSubmitButtonTitle) {
            this.mSubmitButtonTitle = mSubmitButtonTitle;
            return  this;
        }

        public Builder setMdialogMsg(String mdialogMsg) {
            this.mdialogMsg = mdialogMsg;
            return  this;
        }

        public Builder setVisibilityDialogMsg(boolean visibilityDialogMsg) {
            isVisibilityDialogMsg = visibilityDialogMsg;
            return  this;
        }

        public Builder setInputInt(boolean inputInt) {
            isInputInt = inputInt;
            return  this;
        }


        public Builder setmEditHint(String mEditHint) {
            this.mEditHint = mEditHint;
            return  this ;
        }

        public Builder setViewId(int viewId) {
            this.viewId = viewId;
            return  this;
        }

        public Builder setListener(DialogSubmitListener listener) {
            this.mListener = listener;
            return this;
        }


        public  UnTakeDialogFragment  show(){
            UnTakeDialogFragment mUnTakeDialog =UnTakeDialogFragment.newInstance( mTitle,  mSubmitButtonTitle,  mdialogMsg,
                    isVisibilityDialogMsg,  isInputInt, viewId,mEditHint);
            mUnTakeDialog.setOnSubmitClickListener(mListener);
            mUnTakeDialog.show(mFragmentManager,mTag);
            return   mUnTakeDialog;

        };
    }


}
